package palidino76.rs2.player;

import palidino76.rs2.*;
import palidino76.rs2.player.combat.PlayerCombat;
import palidino76.rs2.player.misc.*;
import palidino76.rs2.player.combat.*;
import palidino76.rs2.Engine;
import palidino76.rs2.util.Misc;

public class Magic {


	Player p;

	/**
	 * Constructor
	 */
	public Magic(Player p) {
		this.p = p;
	}

    private static int mainDelayInt = 3;
    private static int mainClickDelay = 1;
    public static int fire = 554;
    public static int water = 555;
    public static int air = 556;
    public static int earth = 557;
    public static int mind = 558;
    public static int body = 559;
    public static int law = 563;
    public static int cosmic = 564;
    public static int death = 560;
    public static int nature = 561;
    public static int chaos = 562;
    public static int blood = 565;
    public static int soul = 566;
    public static int bones = 526;
    private static int hitDamage;

    public static void deleteItems(Player p, int itemId, int amt) {
        Engine.playerItems.deleteItem(p, itemId,
                Engine.playerItems.getItemSlot(p, itemId), amt);
    }

    public static void deleteItems(Player p, int item1id, int item1amt, int item2id, int item2amt) {
        Engine.playerItems.deleteItem(p, item1id,
                Engine.playerItems.getItemSlot(p, item1id), item1amt);
        Engine.playerItems.deleteItem(p, item2id,
                Engine.playerItems.getItemSlot(p, item2id), item2amt);
    }

    public static void deleteItems(Player p, int item1id, int item1amt, int item2id, int item2amt, int item3id, int item3amt) {
        Engine.playerItems.deleteItem(p, item1id,
                Engine.playerItems.getItemSlot(p, item1id), item1amt);
        Engine.playerItems.deleteItem(p, item2id,
                Engine.playerItems.getItemSlot(p, item2id), item2amt);
        Engine.playerItems.deleteItem(p, item3id,
                Engine.playerItems.getItemSlot(p, item3id), item3amt);
    }
    
    public static void deleteItems(Player p, int item1id, int item1amt, int item2id, int item2amt, int item3id, int item3amt, int item4id, int item4amt) {
        Engine.playerItems.deleteItem(p, item1id,
                Engine.playerItems.getItemSlot(p, item1id), item1amt);
        Engine.playerItems.deleteItem(p, item2id,
                Engine.playerItems.getItemSlot(p, item2id), item2amt);
        Engine.playerItems.deleteItem(p, item3id,
                Engine.playerItems.getItemSlot(p, item3id), item3amt);
        Engine.playerItems.deleteItem(p, item4id,
                Engine.playerItems.getItemSlot(p, item4id), item4amt);
    }

    public static String itemName(int itemID) {
        return Engine.items.getItemName(itemID);
    }
    
    public static boolean addItem(Player p, int ItemId, int amt) {
        return Engine.playerItems.addItem(p, ItemId, amt);
    }

    public static boolean hasReq(Player p, int ItemId, int amt) {
        return Engine.playerItems.haveItem(p, ItemId, amt);
    }
    
    public static boolean magicXp(Player p, double xpamt) {
        p.addSkillXP(xpamt, 6);
        return false;
    }

    public static void sendMsg(Player p, String MSG) {
        p.frames.sendMessage(p, MSG);
    }

    public static void anim(Player p, int animID, int time) {
        p.requestAnim(animID, time);
    }

    public static void teleTo(Player p, int x, int y) {
        p.teleportTo(x, y, 0, 4, 4, 8939, 8941, 1576, 0, 1577, 0);
    }

    public static void teleTo(Player p, int x, int y, int h) {
        p.teleportTo(x, y, h, 4, 4, 8939, 8941, 1576, 0, 1577, 0);
    }
    
    public static void ancientTeleTo(Player p, int x, int y) {
        p.teleportTo(x, y, 0, 4, 4, 1979, 0, 392, 0, 0, 0);
    }

    public static void stopMove(Player p) {
        Server.engine.playerMovement.resetWalkingQueue(p);
    }
    
    public static void cast(Player p, int offsetx, int offsety, int moving, int starth, int endh, int speed, int target) {
        p.createGlobalProjectile(p.absY, p.absX, offsety, offsetx, moving,
                starth, endh, speed, target);
    }
    
    public static void face(Player p, int amt) {
        p.requestFaceTo(amt);
    }
    
    public static int magicLvl(Player p) {
        return p.getLevelForXP(6);
    }

    public static void anim(Player p, int animID) {
        p.requestAnim(animID, 0);
    }

    public static void gfa(Player p, int gfxID, int animID) {
        p.requestAnim(animID, 0);
        p.requestGFX(gfxID, 0);
    }

    public static void gfa(Player p, int gfxID, int time, int animID, int time2) {
        p.requestAnim(animID, time2);
        p.requestGFX(gfxID, time);
    }

    public static void gfx(Player p, int gfxID) {
        p.requestGFX(gfxID, 0);
    }

    public static void gfx(Player p, int gfxID, int time) {
        p.requestGFX(gfxID, time);
    }

    public static boolean ancientMagicAB(Player p, int buttonID) {
        int miscAmount;

	if (p.absY >= 3672 && Server.engine.wildernessArea(p.absX, p.absY)) {
		sendMsg(p, "You cannot teleport above 20 wilderness!");
	}
	if (p.AtCastleWars() || p.AtPits() || p.duelFight() || p.inBounty) {
		sendMsg(p, "You cannot teleport out of this mini-game.");
	}

        switch (buttonID) {
            
        case 23: // Ghorrock Teleport
            if (p.magicDelay <= 0) {
                if (magicLvl(p) >= ancientLvlReq[buttonID]) {
                    if (hasReq(p, law, 2) && hasReq(p, water, 8)) {
                        ancientTeleTo(p, 2976, 3874);
                        deleteItems(p, law, 2, water, 8);
                        magicXp(p, ancientSpellXp[buttonID]);
			p.magicDelay = 5;
                    } else {
                        sendMsg(p, "You dont have the required runes to cast this spell.");
                    }
                } else {
                    sendMsg(p, "You need level " + modernLvlReq[buttonID] + " magic to use this spell.");
                }
            } else {
                p.clickDelay = mainClickDelay;
            }
            break;
            
        case 22: // Annakarl Teleport
            if (p.magicDelay <= 0) {
                if (magicLvl(p) >= ancientLvlReq[buttonID]) {
                    if (hasReq(p, law, 2) && hasReq(p, blood, 2)) {
                        ancientTeleTo(p, 3288, 3886);
                        deleteItems(p, law, 2, blood, 2);
                        magicXp(p, ancientSpellXp[buttonID]);
			p.magicDelay = 5;
                    } else {
                        sendMsg(p,
                                "You dont have the required runes to cast this spell.");
                    }
                } else {
                    sendMsg(p,
                            "You need level " + modernLvlReq[buttonID]
                            + " magic to use this spell.");

                }
            } else {
                p.clickDelay = mainClickDelay;
            }
            break;
            
        case 21: // Carrallangar Teleport
            if (p.magicDelay <= 0) {
                if (magicLvl(p) >= ancientLvlReq[buttonID]) {
                    if (hasReq(p, law, 2) && hasReq(p, soul, 2)) {
                        ancientTeleTo(p, 3156, 3666);
                        deleteItems(p, law, 2, soul, 2);
                        magicXp(p, ancientSpellXp[buttonID]);
			p.magicDelay = 5;
                    } else {
                        sendMsg(p,
                                "You dont have the required runes to cast this spell.");
                        
                    }
                } else {
                    sendMsg(p,
                            "You need level " + modernLvlReq[buttonID]
                            + " magic to use this spell.");
                    
                }
            } else {
                p.clickDelay = mainClickDelay;
            }
            break;
            
        case 20: // Dareeyak Teleport
            if (p.magicDelay <= 0) {
                if (magicLvl(p) >= ancientLvlReq[buttonID]) {
                    if (hasReq(p, law, 2) && hasReq(p, air, 2)
                            && hasReq(p, fire, 3)) {
                        ancientTeleTo(p, 2963, 3696);
                        deleteItems(p, law, 2, air, 2, fire, 3);
                        magicXp(p, ancientSpellXp[buttonID]);
			p.magicDelay = 5;
                    } else {
                        sendMsg(p,
                                "You dont have the required runes to cast this spell.");
                        
                    }
                } else {
                    sendMsg(p,
                            "You need level " + modernLvlReq[buttonID]
                            + " magic to use this spell.");
                    
                }
            } else {
                p.clickDelay = mainClickDelay;
            }
            break;
            
        case 19: // Lassar Teleport
            if (p.magicDelay <= 0) {
                if (magicLvl(p) >= ancientLvlReq[buttonID]) {
                    if (hasReq(p, law, 2) && hasReq(p, water, 4)) {
                        ancientTeleTo(p, 3008, 3477);
                        deleteItems(p, law, 2, water, 4);
                        magicXp(p, ancientSpellXp[buttonID]);
			p.magicDelay = 5;
                    } else {
                        sendMsg(p,
                                "You dont have the required runes to cast this spell.");
                        
                    }
                } else {
                    sendMsg(p,
                            "You need level " + modernLvlReq[buttonID]
                            + " magic to use this spell.");
                    
                }
            } else {
                p.clickDelay = mainClickDelay;
            }
            break;
            
        case 18: // Kharyll Teleport
            if (p.magicDelay <= 0) {
                if (magicLvl(p) >= ancientLvlReq[buttonID]) {
                    if (hasReq(p, law, 2) && hasReq(p, blood, 1)) {
                        ancientTeleTo(p, 3491, 3471);
                        deleteItems(p, law, 2, blood, 1);
                        magicXp(p, ancientSpellXp[buttonID]);
			p.magicDelay = 5;
                    } else {
                        sendMsg(p,
                                "You dont have the required runes to cast this spell.");
                        
                    }
                } else {
                    sendMsg(p,
                            "You need level " + modernLvlReq[buttonID]
                            + " magic to use this spell.");
                    
                }
            } else {
                p.clickDelay = mainClickDelay;
            }
            break;
            
        case 17: // Senntisten Teleport
            if (p.magicDelay <= 0) {
                if (magicLvl(p) >= ancientLvlReq[buttonID]) {
                    if (hasReq(p, law, 2) && hasReq(p, soul, 1)) {
                        ancientTeleTo(p, 3322, 3337);
                        deleteItems(p, law, 2, soul, 1);
                        magicXp(p, ancientSpellXp[buttonID]);
			p.magicDelay = 5;
                    } else {
                        sendMsg(p,
                                "You dont have the required runes to cast this spell.");
                        
                    }
                } else {
                    sendMsg(p,
                            "You need level " + modernLvlReq[buttonID]
                            + " magic to use this spell.");
                    
                }
            } else {
                p.clickDelay = mainClickDelay;
            }
            break;
            
        case 16: // Paddawa Teleport -- FIX
            if (p.magicDelay <= 0) {
                if (magicLvl(p) >= ancientLvlReq[buttonID]) {
                    if (hasReq(p, law, 2) && hasReq(p, fire, 1) && hasReq(p, air, 1)) {
                        ancientTeleTo(p, 3322, 3337);
                        deleteItems(p, law, 2, soul, 1);
                        magicXp(p, ancientSpellXp[buttonID]);
			p.magicDelay = 5;
                    } else {
                        sendMsg(p,
                                "You dont have the required runes to cast this spell.");
                        
                    }
                } else {
                    sendMsg(p,
                            "You need level " + modernLvlReq[buttonID]
                            + " magic to use this spell.");
                    
                }
            } else {
                p.clickDelay = mainClickDelay;
            }
            break;

        default:
            return false;
        }
        return true;
    }
    
    public static boolean modernMagicAB(Player p, int buttonID) {
        int miscAmount;

	if (p.absY >= 3672 && Server.engine.wildernessArea(p.absX, p.absY)) {
		sendMsg(p, "You cannot teleport above 20 wilderness!");
	}
	if (p.AtCastleWars() || p.AtPits() || p.duelFight() || p.inBounty) {
		sendMsg(p, "You cannot teleport out of this mini-game.");
	}

        switch (buttonID) {

        case 15: // Varrock Teleport
            if (p.magicDelay <= 0) {
                if (magicLvl(p) >= modernLvlReq[buttonID]) {
                    if (hasReq(p, air, 3) && hasReq(p, law, 1)
                            && hasReq(p, fire, 1)) {
                        teleTo(p, 3212, 3434);
                        deleteItems(p, air, 3, law, 1, fire, 1);
                        magicXp(p, modernSpellXp[buttonID]);
			p.magicDelay = 5;
                    } else {
                        sendMsg(p,
                                "You dont have the required runes to cast this spell.");
                        
                    }
                } else {
                    sendMsg(p,
                            "You need level " + modernLvlReq[buttonID]
                            + " magic to use this spell.");
                    
                }
            } else {
                p.clickDelay = mainClickDelay;
            }
            break;

        case 18: // Lumbridge Teleport
            if (p.magicDelay <= 0) {
                if (magicLvl(p) >= modernLvlReq[buttonID]) {
                    if (hasReq(p, air, 3) && hasReq(p, law, 1)
                            && hasReq(p, earth, 1)) {
                        teleTo(p, 3221, 3219);
                        deleteItems(p, air, 3, law, 1, earth, 1);
                        magicXp(p, modernSpellXp[buttonID]);
			p.magicDelay = 5;
                    } else {
                        sendMsg(p,
                                "You dont have the required runes to cast this spell.");
                        
                    }
                } else {
                    sendMsg(p,
                            "You need level " + modernLvlReq[buttonID]
                            + " magic to use this spell.");
                    
                }
            } else {
                p.clickDelay = mainClickDelay;
            }
            break;

        case 21: // Falador Teleport
            if (p.magicDelay <= 0) {
                if (magicLvl(p) >= modernLvlReq[buttonID]) {
                    if (hasReq(p, air, 3) && hasReq(p, law, 1)
                            && hasReq(p, water, 1)) {
                        teleTo(p, 2965, 3380);
                        deleteItems(p, water, 1, law, 1, air, 3);
                        magicXp(p, modernSpellXp[buttonID]);
			p.magicDelay = 5;
                    } else {
                        sendMsg(p,
                                "You dont have the required runes to cast this spell.");
                        
                    }
                } else {
                    sendMsg(p,
                            "You need level " + modernLvlReq[buttonID]
                            + " magic to use this spell.");
                    
                }
            } else {
                p.clickDelay = mainClickDelay;
            }
            break;

        case 26: // Camelot teleport
            if (p.magicDelay <= 0) {
                if (magicLvl(p) >= modernLvlReq[buttonID]) {
                    if (hasReq(p, air, 5) && hasReq(p, law, 1)) {
                        teleTo(p, 2757, 3477);
                        deleteItems(p, law, 1, air, 5);
                        magicXp(p, modernSpellXp[buttonID]);
			p.magicDelay = 5;
                    } else {
                        sendMsg(p,
                                "You dont have the required runes to cast this spell.");
                        
                    }
                } else {
                    sendMsg(p,
                            "You need level " + modernLvlReq[buttonID]
                            + " magic to use this spell.");
                    
                }
            } else {
                p.clickDelay = mainClickDelay;
            }
            break;

        case 32: // Ardougne Teleport
            if (p.magicDelay <= 0) {
                if (magicLvl(p) >= modernLvlReq[buttonID]) {
                    if (hasReq(p, water, 2) && hasReq(p, law, 2)) {
                        teleTo(p, 2662, 3305);
                        deleteItems(p, law, 2, water, 2);
                        magicXp(p, modernSpellXp[buttonID]);
			p.magicDelay = 5;
                    } else {
                        sendMsg(p,
                                "You dont have the required runes to cast this spell.");
                        
                    }
                } else {
                    sendMsg(p,
                            "You need level " + modernLvlReq[buttonID]
                            + " magic to use this spell.");
                    
                }
            } else {
                p.clickDelay = mainClickDelay;
            }
            break;

        case 37:
            if (p.magicDelay <= 0) {
                if (magicLvl(p) >= modernLvlReq[buttonID]) {
                    if (hasReq(p, earth, 2) && hasReq(p, law, 2)) {
                        teleTo(p, 2545, 3112);
                        deleteItems(p, law, 2, earth, 2);
                        magicXp(p, modernSpellXp[buttonID]);
			p.magicDelay = 5;
                    } else {
                        sendMsg(p,
                                "You dont have the required runes to cast this spell.");
                        
                    }
                } else {
                    sendMsg(p,
                            "You need level " + modernLvlReq[buttonID]
                            + " magic to use this spell.");
                    
                }
            } else {
                p.clickDelay = mainClickDelay;
            }
            break;


        case 44:
            if (p.magicDelay <= 0) {
                if (magicLvl(p) >= modernLvlReq[buttonID]) {
                    if (hasReq(p, fire, 2) && hasReq(p, law, 2)) {
                        teleTo(p, 2891, 3678);
                        deleteItems(p, law, 2, fire, 2);
                        magicXp(p, modernSpellXp[buttonID]);
			p.magicDelay = 5;
                    } else {
                        sendMsg(p,
                                "You dont have the required runes to cast this spell.");
                        
                    }
                } else {
                    sendMsg(p,
                            "You need level " + modernLvlReq[buttonID]
                            + " magic to use this spell.");
                    
                }
            } else {
                p.clickDelay = mainClickDelay;
            }
            break;

        case 47:
            if (p.magicDelay <= 0) {
                if (magicLvl(p) >= modernLvlReq[buttonID]) {
                    if (hasReq(p, fire, 2) && hasReq(p, law, 2)
                            && hasReq(p, water, 2) && hasReq(p, 1963, 1)) {
                        teleTo(p, 2755, 2784);
                        deleteItems(p, water, 2, law, 2, fire, 2, 1963, 1);
                        magicXp(p, modernSpellXp[buttonID]);
			p.magicDelay = 5;
                    } else {
                        sendMsg(p,
                                "You dont have the required items to cast this spell.");
                        
                    }
                } else {
                    sendMsg(p,
                            "You need level " + modernLvlReq[buttonID]
                            + " magic to use this spell.");
                    
                }
            } else {
                sendMsg(p, "You need to wait before casting a spell again.");
                p.clickDelay = mainClickDelay;
            }
            break;

        default:
            return false;
        }
        return true;
    }

    public static void MagicOnItemHandle(Player p, int packetId, int packetSize) {
        if ((p == null) || (p.stream == null)) {
            return;
        }

        int itemID = p.stream.readUnsignedWordBigEndianA();
        int i2 = p.stream.readUnsignedWordBigEndian();
        int itemSlot = p.stream.readUnsignedWordBigEndian();
        int i4 = p.stream.readDWord();
        int spellID = p.stream.readDWord();
	int interfaceId = p.stream.readUnsignedWord();

        //default:
            Misc.println("PlayerID - InterfaceID: " + interfaceId
                    + " - ButtonID " + spellID + ".");

        if (p.items[itemSlot] != itemID) {
            return;
        }

        switch (spellID) {

        case 184593921: // Plank make
                if (magicLvl(p) >= 86) {
                    if (hasReq(p, 9075, 2) && hasReq(p, earth, 15) && hasReq(p, nature, 1)) {
			if (itemID == 1511) {
                        	addItem(p, 960, 1);
                        	p.requestGFX(1063, 0);
                        	p.requestAnim(6298, 0);
                        	deleteItems(p, 9075, 2, nature, 1, earth, 15);
                        	Engine.playerItems.deleteItem(p, itemID, itemSlot, 1);
                        	magicXp(p, 150);

			} else if (itemID == 1521) {
                        	addItem(p, 8778, 1);
                        	p.requestGFX(1063, 0);
                        	p.requestAnim(6298, 0);
                        	deleteItems(p, 9075, 2, nature, 1, earth, 15);
                        	Engine.playerItems.deleteItem(p, itemID, itemSlot, 1);
                        	magicXp(p, 150);

			} else if (itemID == 6333) {
                        	addItem(p, 8780, 1);
                        	p.requestGFX(1063, 0);
                        	p.requestAnim(6298, 0);
                        	deleteItems(p, 9075, 2, nature, 1, earth, 15);
                        	Engine.playerItems.deleteItem(p, itemID, itemSlot, 1);
                        	magicXp(p, 150);

			} else if (itemID == 6332) {
                        	addItem(p, 8782, 1);
                        	p.requestGFX(1063, 0);
                        	p.requestAnim(6298, 0);
                        	deleteItems(p, 9075, 2, nature, 1, earth, 15);
                        	Engine.playerItems.deleteItem(p, itemID, itemSlot, 1);
                        	magicXp(p, 150);
			} else {
			    sendMsg(p, "You need to cast this spell on a log.");
			}

                    } else {
                        	sendMsg(p, "You dont have the required runes to cast this spell.");
                    }
                } else {
                    sendMsg(p, "You need level 86 magic to use this spell.");
                }
            break;

        case 570474496: // High-Alchemy
            if (p.magicDelay <= 0) {
                if (magicLvl(p) >= modernLvlReq[34]) {
                    if (hasReq(p, nature, 1) && hasReq(p, fire, 5)) {
                        gfa(p, 113, 713);
                        //addItem(p, 995, Engine.items.itemLists[itemID].highAlch);
						addItem(p, 995, Misc.random(2500));
                        deleteItems(p, fire, 5, nature, 1);
                        Engine.playerItems.deleteItem(p, itemID, itemSlot, 1);
                        magicXp(p, modernSpellXp[34]);
                        p.magicDelay = mainDelayInt;
                    } else {
                        sendMsg(p,
                                "You dont have the required runes to cast this spell.");
                        p.clickDelay = mainClickDelay;
                    }
                } else {
                    sendMsg(p,
                            "You need level " + modernLvlReq[34]
                            + " magic to use this spell.");
                    p.clickDelay = mainClickDelay;
                }
            } else {
                sendMsg(p, "You need to wait before casting a spell again.");
                p.clickDelay = mainClickDelay;
            }
            break;

        case 218152960: // Low-Alchemy
            if (p.magicDelay <= 0) {
                if (magicLvl(p) >= modernLvlReq[12]) {
                    if (hasReq(p, nature, 1) && hasReq(p, fire, 3)) {
                        gfa(p, 112, 712);
                        addItem(p, 995, Engine.items.itemLists[itemID].lowAlch);
                        deleteItems(p, fire, 3, nature, 1);
                        Engine.playerItems.deleteItem(p, itemID, itemSlot, 1);
                        magicXp(p, modernSpellXp[12]);
                        p.magicDelay = mainDelayInt;
                    } else {
                        sendMsg(p,
                                "You dont have the required runes to cast this spell.");
                        p.clickDelay = mainClickDelay;
                    }
                } else {
                    sendMsg(p,
                            "You need level " + modernLvlReq[12]
                            + " magic to use this spell.");
                    p.clickDelay = mainClickDelay;
                }
            } else {
                sendMsg(p, "You need to wait before casting a spell again.");
                p.clickDelay = mainClickDelay;
            }
            break;

        }
    }

    public static void magicAop(Player p, int packetId, int packetSize) {

	    if (!p.magicOnPlayer) {
		PlayerMethods pm = new PlayerMethods(p);
		pm.setMagicPlayer(true);
	    }

        	int interfaceId = p.interfaceID;
        	int playerId = p.pID;
        	Player p2 = Engine.players[playerId];
		p.enemyIndex = playerId;
		PlayerMethods pm = new PlayerMethods(p);
		PlayerMagic playerM = new PlayerMagic(p);
		PlayerCombat PC = new PlayerCombat(p);

        	int offsetX = (p.absX - p2.absX) * -1;
        	int offsetY = (p.absY - p2.absY) * -1;
		int casterX = p.absX;
		int casterY = p.absY;


	if (p == null || p2 == null || p.isDead || p2.isDead) {
		p.requestFaceTo(65535);
		p.resetAttack();
		return;
	}

	if (p.duelFight() && p2.duelFight()) {
		if (p2.duelFriend == p.playerId) {
			if (p.countType > -1) {
				p.resetAttack();
                        	face(p, p2.playerId + 32768);
                            	stopMove(p);
				return;
			}
		} else {
			p.frames.sendMessage(p, "You cannot attack this player!");
			p.resetAttack();
			return;
		}
	}
	if (p.equipment[1] == 4041 && p2.equipment[1] == 4041) {
	   	p.frames.sendMessage(p, "You cannot attack a player that is on your team.");
		p.resetAttack();
	    return;
	}
	if (p.equipment[1] == 4042 && p2.equipment[1] == 4042) {
	   	p.frames.sendMessage(p, "You cannot attack a player that is on your team.");
		p.resetAttack();
	   	return;
	}

	if (!PC.isInWildRange(p) && !p.duelFight()) {
	    if (interfaceId != 430) {
		p.requestFaceTo(p.enemyIndex + 32768);
		p.requestFaceTo(65535);
		stopMove(p);
	   	p.frames.sendMessage(p, "Your level difference is too great!");
	   	p.frames.sendMessage(p, "You need to move further into the Wilderness.");
		p.resetAttack();
	   	return;
	    }
	}

	if (p.inEdgePk() && p2.inEdgePk() || !p.multiZone() && !p2.multiZone()) {
	    if (interfaceId != 430) {
		if (p2.combatWith == 0) {
	    		p2.cmbResetTimer = 10;
			p2.combatWith = p.playerId;
		}
	    }
	}

	if (p.inEdgePk() && p2.inEdgePk() || !p.multiZone() && !p2.multiZone()) {
	    if (interfaceId != 430) {
		if (p.combatWith != p2.playerId && p.combatWith != 0) {
			p.requestFaceTo(p.enemyIndex + 32768);
			p.requestFaceTo(65535);
                        stopMove(p);
			p.resetAttack();
			p.frames.sendMessage(p, "You are already in combat.");
			return;
		}
		
			/*if(p.inBounty) {
	    Player p3 = Engine.players[p2.attackedBy];
	    if(p2.attackedBy > 0 && p2.playerId != p.bhTarget && p2.bhTarget != p.playerId &&
	       p2.attackedBy != p.playerId && p2.attackedBy != p2.bhTarget &&
	       p3.bhTarget != p2.playerId) {
		   p.resetAttack();
		p.frames.sendMessage(p, "This player is already in combat with another person.");
		p.enemyId = 0;
	        return;
	    }
	}
	p2.attackedBy = p.playerId;*/
		if (p2.combatWith != p.playerId && p2.combatWith != 0) {
			p.resetAttack();
                        stopMove(p);
                        face(p, p2.playerId + 32768);
			p.frames.sendMessage(p, "That player is already in combat.");
			return;
		}
	    }
	}

      switch (interfaceId) {

        case 430: // Lunar magic
            switch (p.clickId) {

	    case 1:
			stopMove(p);
                    if (magicLvl(p) >= 68) {
                        if (hasReq(p, 557, 10) && hasReq(p, 9075, 3) && hasReq(p, law, 1)) {
			    if (p2.isPoisoned) {
                                face(p, p2.playerId + 32768);
                                stopMove(p);
				p.requestAnim(4411, 0);
				p.combatDelay = 3;
				p2.isPoisoned = false;
				p2.poisonDelay = 0;
				p.spellType = p.interfaceId;
                            	p2.requestGFX(738, 170);
                                deleteItems(p, 561, 1, 9075, 3, law, 2);
                                magicXp(p, 4100);
                                sendMsg(p, "You cure "+p2.username+" from poison.");
                                sendMsg(p2, "You have been cured of poison by "+p.username+".");
				p.frames.playSound(p2, 2887, 1, 0);
				p.frames.playSound(p, 2887, 1, 0);
				pm.setMagicPlayer(false);
			    } else {
                                face(p, p2.playerId + 32768);
				sendMsg(p, "That player isn't poisoned!");
				pm.setMagicPlayer(false);
			    }
                        } else {
                            	sendMsg(p, "You dont have the required runes for this spell.");
				pm.setMagicPlayer(false);
                        }
                    } else {
                        sendMsg(p, "You need level 68 magic to use this spell.");
			pm.setMagicPlayer(false);
                    }
			break;

	    case 9:
			stopMove(p);
                    if (magicLvl(p) >= 75) {
                        if (hasReq(p, 559, 5) && hasReq(p, 9075, 2) && hasReq(p, 564, 2)) {
				p.frames.setTab(p, 79, 523);
				p.frames.setString(p, "Stats for:", 523, 98);
				p.frames.setString(p, Misc.optimizeText(p2.username), 523, 99);
				p.frames.setString(p, ""+p2.skillLvl[0], 523, 1);
				p.frames.setString(p, ""+p2.skillLvl[0], 523, 2);
				p.frames.setString(p, ""+p2.skillLvl[1], 523, 25);
				p.frames.setString(p, ""+p2.skillLvl[1], 523, 26);
				p.frames.setString(p, ""+p2.skillLvl[2], 523, 13);
				p.frames.setString(p, ""+p2.skillLvl[2], 523, 14);
				p.frames.setString(p, ""+p2.skillLvl[3], 523, 5);
				p.frames.setString(p, ""+p2.skillLvl[3], 523, 6);
				p.frames.setString(p, ""+p2.skillLvl[4], 523, 37);
				p.frames.setString(p, ""+p2.skillLvl[4], 523, 38);
				p.frames.setString(p, ""+p2.skillLvl[5], 523, 49);
				p.frames.setString(p, ""+p2.skillLvl[5], 523, 50);
				p.frames.setString(p, ""+p2.skillLvl[6], 523, 61);
				p.frames.setString(p, ""+p2.skillLvl[6], 523, 62);
				p.frames.setString(p, ""+p2.skillLvl[7], 523, 45);
				p.frames.setString(p, ""+p2.skillLvl[7], 523, 46);
				p.frames.setString(p, ""+p2.skillLvl[8], 523, 69);
				p.frames.setString(p, ""+p2.skillLvl[8], 523, 70);
				p.frames.setString(p, ""+p2.skillLvl[9], 523, 65);
				p.frames.setString(p, ""+p2.skillLvl[9], 523, 66);
				p.frames.setString(p, ""+p2.skillLvl[10], 523, 33);
				p.frames.setString(p, ""+p2.skillLvl[10], 523, 34);
				p.frames.setString(p, ""+p2.skillLvl[11], 523, 57);
				p.frames.setString(p, ""+p2.skillLvl[11], 523, 58);
				p.frames.setString(p, ""+p2.skillLvl[12], 523, 53);
				p.frames.setString(p, ""+p2.skillLvl[12], 523, 54);
				p.frames.setString(p, ""+p2.skillLvl[13], 523, 21);
				p.frames.setString(p, ""+p2.skillLvl[13], 523, 22);
				p.frames.setString(p, ""+p2.skillLvl[14], 523, 9);
				p.frames.setString(p, ""+p2.skillLvl[14], 523, 10);
				p.frames.setString(p, ""+p2.skillLvl[15], 523, 29);
				p.frames.setString(p, ""+p2.skillLvl[15], 523, 30);
				p.frames.setString(p, ""+p2.skillLvl[16], 523, 17);
				p.frames.setString(p, ""+p2.skillLvl[16], 523, 18);
				p.frames.setString(p, ""+p2.skillLvl[17], 523, 41);
				p.frames.setString(p, ""+p2.skillLvl[17], 523, 42);
				p.frames.setString(p, ""+p2.skillLvl[18], 523, 77);
				p.frames.setString(p, ""+p2.skillLvl[18], 523, 78);
				p.frames.setString(p, ""+p2.skillLvl[19], 523, 81);
				p.frames.setString(p, ""+p2.skillLvl[19], 523, 82);
				p.frames.setString(p, ""+p2.skillLvl[20], 523, 73);
				p.frames.setString(p, ""+p2.skillLvl[20], 523, 74);
				p.frames.setString(p, ""+p2.skillLvl[21], 523, 85);
				p.frames.setString(p, ""+p2.skillLvl[21], 523, 86);
				p.frames.setString(p, ""+p2.skillLvl[22], 523, 89);
				p.frames.setString(p, ""+p2.skillLvl[22], 523, 90);
				p.frames.setString(p, ""+p2.skillLvl[23], 523, 93);
				p.frames.setString(p, ""+p2.skillLvl[23], 523, 94);
                                face(p, p2.playerId + 32768);
                                stopMove(p);
				p.requestAnim(6293, 0);
				p2.isPoisoned = false;
				p2.poisonDelay = 0;
				p.spellType = p.interfaceId;
                            	p2.requestGFX(736, 100);
                            	p.requestGFX(1060, 100);
                                deleteItems(p, 559, 5, 9075, 2, 564, 2);
                                magicXp(p, 2400);
                                p.frames.sendMessage(p, "You spy on "+p2.username+"'s stats.");
                                p.frames.sendMessage(p2, "Your stats are being spyed on by "+p.username+".");
				p.frames.playSound(p2, 2890, 1, 0);
				p.frames.playSound(p, 2890, 1, 0);
				pm.setMagicPlayer(false);
                        } else {
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
                        sendMsg(p, "You need level 68 magic to use this spell.");
                    }
			break;

	    case 19:
			stopMove(p);
                    if (magicLvl(p) >= 93) {
                        if (hasReq(p, death, 2) && hasReq(p, 9075, 3) && hasReq(p, earth, 10)) {
			    if (p.vengUseDelay <= 0 || p2.vengDelay <= 0) {
                                face(p, p2.playerId + 32768);
                                stopMove(p);
				p.requestAnim(4411, 0);
				p.combatDelay = 3;
				p.spellType = p.interfaceId;
                            	p2.requestGFX(725, 100);
                                deleteItems(p, death, 2, 9075, 3, earth, 10);
                                magicXp(p, 5000);
                                sendMsg(p, "You cast vengeance on "+p2.username+".");
                                sendMsg(p2, "Vengeance has been casted on you by "+p.username+".");
				p2.vengDelay = 50;
				p2.usingVeng = true;
				p.vengDelay = 50;
				p.vengUseDelay = 50;
				p2.vengUseDelay = 60;
				p.frames.playSound(p2, 2908, 1, 0);
				p.frames.playSound(p, 2908, 1, 0);
				pm.setMagicPlayer(false);
			    } else {
                                face(p, p2.playerId + 32768);
				sendMsg(p, "You can only use vengeance every 30 seconds!");
				pm.setMagicPlayer(false);
			    }
                        } else {
                            	sendMsg(p, "You dont have the required runes for this spell.");
				pm.setMagicPlayer(false);
                        }
                    } else {
                        sendMsg(p, "You need level 93 magic to use this spell.");
			pm.setMagicPlayer(false);
                    }
			break;

	    case 29:
			stopMove(p);
                    if (magicLvl(p) >= 92) {
                        if (hasReq(p, blood, 1) && hasReq(p, 9075, 3) && hasReq(p, law, 3)) {
			   if (p.skillLvl[3] > p.skillLvl[3] / 4 && p.skillLvl[3] > p2.getLevelForXP(3) - p2.skillLvl[3]) {
			     if (p2.skillLvl[3] != p2.getLevelForXP(3)) {
                                face(p, p2.playerId + 32768);
                                stopMove(p);
				p.requestAnim(4411, 0);
				p.combatDelay = 3;
				p.spellType = p.interfaceId;
				p.appendHit(p2.getLevelForXP(3) - p2.skillLvl[3], 0);
				p2.skillLvl[3] += p2.getLevelForXP(3) - p2.skillLvl[3];
				p.frames.setSkillLvl(p2, 3);
				p.frames.setSkillLvl(p, 3);
                            	p2.requestGFX(738, 170);
                                deleteItems(p, blood, 1, 9075, 3, law, 3);
                                magicXp(p, 4500);
                                sendMsg(p, "You heal "+p2.username+".");
                                sendMsg(p2, "You have been healed by "+p.username+".");
				p.frames.playSound(p2, 2895, 1, 0);
				p.frames.playSound(p, 2895, 1, 0);
				pm.setMagicPlayer(false);
			    } else {
                                face(p, p2.playerId + 32768);
				sendMsg(p, "The other players hitpoints are already full.");
				pm.setMagicPlayer(false);
			    }
			    } else {
                                face(p, p2.playerId + 32768);
				sendMsg(p, "Your Health Points are too low to heal their HP amount!");
				pm.setMagicPlayer(false);
			    }
                        } else {
                            	sendMsg(p, "You dont have the required runes for this spell.");
				pm.setMagicPlayer(false);
                        }
                    } else {
                        sendMsg(p, "You need level 92 magic to use this spell.");
			pm.setMagicPlayer(false);
                    }
			break;

	    case 5:
			stopMove(p);
                    if (magicLvl(p) >= 91) {
                        if (hasReq(p, 561, 1) && hasReq(p, 9075, 3) && hasReq(p, law, 2)) {
			    if (p2.specAmount < 501) {
                                face(p, p2.playerId + 32768);
                                stopMove(p);
				p.requestAnim(4411, 0);
				p.combatDelay = 3;
				p.spellType = p.interfaceId;
				p.specAmount -= 500;
				p2.specAmount += 500;
                            	p2.requestGFX(734, 100);
                                deleteItems(p, 561, 1, 9075, 3, law, 2);
                                magicXp(p, 4100);
                                sendMsg(p, "You transfer 50 your special attack energy to "+p2.username+".");
                                sendMsg(p2, "You have been given 50 special attack energy by "+p.username+".");
				p.frames.playSound(p2, 2903, 1, 0);
				p.frames.playSound(p, 2903, 1, 0);
			    } else {
                                face(p, p2.playerId + 32768);
				sendMsg(p, "The other player already has enough special attack!");
				pm.setMagicPlayer(false);
			    }
                        } else {
                            	sendMsg(p, "You dont have the required runes for this spell.");
				pm.setMagicPlayer(false);
                        }
                    } else {
                        sendMsg(p, "You need level 91 magic to use this spell.");
			pm.setMagicPlayer(false);
                    }
			break;

            default:
                Misc.println("Unhandled Lunar button: " + p.clickId + ".");
            }
            break;

        default:
            Misc.println(
                    "PlayerID " + playerId + " - InterfaceID " + interfaceId
                    + " - ButtonID " + p.clickId + ".");

        }

    if (p.wildernessArea(p2.absX, p2.absY) && p.wildernessArea(p.absX, p.absY) || p.duelFight() && p2.duelFight()) {
		p2.combatWith = p.playerId;
	if (!p.duelFight() && !p.AtPits() && !p.AtCastleWars() && !p.inBounty) {
		if (!p.isSkulled && p2.enemyIndex != p.playerId) {
			p.headIconSkull = 0;
			p.skullVanishDelay = 1200;
			p.isSkulled = true;
			p.appearanceUpdateReq = true;
			p.updateReq = true;
		}
	}
      switch (interfaceId) {

        case 193: // Ancients magic
            switch (p.clickId) {

	    case 3:
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= ancientLvlReq[p.clickId]) {
                        if (hasReq(p, death, 4) && hasReq(p, water, 6) && hasReq(p, blood, 2)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				Server.engine.playerMovement.resetWalkingQueue(p2);
				p.magicSpellHit = true;
				p2.freezeDelay = 20;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCastDmgDelay = 5;
                                face(p, p2.playerId + 32768);
                                stopMove(p);
				deleteItems(p, death, 4, water, 6, blood, 2);
				p.frames.playSound(p2, 171, 1, 0);
				p.frames.playSound(p, 171, 1, 0);
				p.autoCastDelay = 5;
				p.requestAnim(1979, 0);
				p.autoCast[1] = 16;
				p.autoCast[0] = 0;
				p.combatDelay = 5;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + ancientLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
			break;
                
            case 7: // Blood Barrage
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= ancientLvlReq[p.clickId]) {
                        if (hasReq(p, death, 4) && hasReq(p, soul, 1) && hasReq(p, blood, 4)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCastDmgDelay = 5;
                                face(p, p2.playerId + 32768);
                                stopMove(p);
				deleteItems(p, death, 4, water, 6, blood, 2);
				p.autoCastDelay = 5;
				p.requestAnim(1979, 0);
				p.autoCast[1] = 15;
				p.autoCast[0] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + ancientLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
                break;
                
            case 15: // Shadow Barrage
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= ancientLvlReq[p.clickId]) {
                        if (hasReq(p, death, 4) && hasReq(p, soul, 3) && hasReq(p, blood, 2) && hasReq(p, air, 4)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCastDmgDelay = 5;
                                face(p, p2.playerId + 32768);
                                stopMove(p);
				deleteItems(p, death, 2, soul, 2, blood, 2, air, 2);
				p.autoCastDelay = 5;
				p.requestAnim(1979, 0);
				p.autoCast[1] = 14;
				p.autoCast[0] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + ancientLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
                break;
                
            case 11: // Smoke Barrage
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= ancientLvlReq[p.clickId]) {
                        if (hasReq(p, death, 4) && hasReq(p, fire, 4) && hasReq(p, blood, 2) && hasReq(p, air, 4)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCastDmgDelay = 5;
                                face(p, p2.playerId + 32768);
                                stopMove(p);
				deleteItems(p, death, 4, fire, 4, blood, 2, air, 4);
				p.autoCastDelay = 5;
				p.frames.playSound(p2, 183, 1, 0);
				p.frames.playSound(p, 183, 1, 0);
				p.requestAnim(1979, 0);
				p.autoCast[1] = 13;
				p.autoCast[0] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + ancientLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
                break;
                
            case 1: // Ice blitz
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= ancientLvlReq[p.clickId]) {
                        if (hasReq(p, death, 2) && hasReq(p, water, 3) && hasReq(p, blood, 2)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				Server.engine.playerMovement.resetWalkingQueue(p2);
				p.magicSpellHit = true;
				p2.freezeDelay = 15;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCastDmgDelay = 6;
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                                gfx(p, 366, 0);
				p.frames.playSound(p, 171, 1, 0);
				p.frames.playSound(p2, 171, 1, 0);
				p.autoCastDelay = 5;
				p.requestAnim(1978, 0);
                                deleteItems(p, death, 2, water, 3, blood, 2);
				p.autoCast[1] = 12;
				p.autoCast[0] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + ancientLvlReq[p.clickId] + " magic to use this spell.");
                    }
			stopMove(p);
                }
                break;
                
            case 5: // Blood blitz
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= ancientLvlReq[p.clickId]) {
                        if (hasReq(p, death, 2) && hasReq(p, blood, 2)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCastDmgDelay = 5;
                                face(p, p2.playerId + 32768);
                                stopMove(p);
				p.autoCastDelay = 6;
				p.requestAnim(1978, 0);
                                deleteItems(p, death, 2, blood, 4);
				p.autoCast[1] = 11;
				p.autoCast[0] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
				p.frames.addProjectile(p, casterY, casterX, offsetY, offsetX, 50, 100, 374, 46, 31, p2.playerId, true);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + ancientLvlReq[p.clickId] + " magic to use this spell.");
                    }
			stopMove(p);
                }
                break;

            case 2: // Ice Burst
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= ancientLvlReq[p.clickId]) {
                        if (hasReq(p, death, 2) && hasReq(p, water, 4) && hasReq(p, chaos, 4)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				Server.engine.playerMovement.resetWalkingQueue(p2);
				p.magicSpellHit = true;
				p2.freezeDelay = 8;
			    } else {
				p.magicSpellHit = false;
			    }
				p.autoCastDmgDelay = 5;
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                            	deleteItems(p, death, 2, chaos, 4, water, 4);
				p.frames.playSound(p2, 171, 1, 0);
				p.frames.playSound(p, 171, 1, 0);
				p.autoCastDelay = 5;
				p.autoCast[0] = 0;
				p.requestAnim(1979, 0);
				p.autoCast[1] = 8;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + ancientLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
			break;
                                
            default:
                Misc.println("Unhandled Ancients button: " + p.clickId + ".");
            }
            break;

        case 192: // Modern magic
            switch (p.clickId) {
            case 1: // Wind Strike
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= modernLvlReq[p.clickId]) {
                        if (hasReq(p, mind, 1) && hasReq(p, air, 1)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                            	deleteItems(p, air, 1, mind, 1);
				p.frames.playSound(p2, 220, 1, 0);
				p.frames.playSound(p, 220, 1, 0);
				p.autoCast[0] = 1;
				p.autoCast[1] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
				playerM.autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 90, 100, 91, 5, p2.playerId, 4);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + modernLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
                break;

            case 4: // Water Strike
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= modernLvlReq[p.clickId]) {
                        if (hasReq(p, mind, 1) && hasReq(p, air, 1) && hasReq(p, water, 1)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                            	deleteItems(p, air, 1, mind, 1, water, 1);
				p.frames.playSound(p2, 211, 1, 0);
				p.frames.playSound(p, 211, 1, 0);
				p.autoCast[0] = 2;
				p.autoCast[1] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
				playerM.autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 93, 100, 94, 5, p2.playerId, 4);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + modernLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
                break;

            case 6: // Earth Strike
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= modernLvlReq[p.clickId]) {
                        if (hasReq(p, mind, 1) && hasReq(p, air, 2) && hasReq(p, earth, 2)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                            	deleteItems(p, air, 1, mind, 1, earth, 2);
				p.frames.playSound(p2, 132, 1, 0);
				p.frames.playSound(p, 132, 1, 0);
				p.autoCast[0] = 3;
				p.autoCast[1] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
				playerM.autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 96, 100, 97, 5, p2.playerId, 4);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + modernLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
                break;

            case 8: // Fire Strike
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= modernLvlReq[p.clickId]) {
                        if (hasReq(p, mind, 1) && hasReq(p, air, 2) && hasReq(p, fire, 3)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                            	deleteItems(p, air, 2, mind, 1, fire, 3);
				p.frames.playSound(p2, 160, 1, 0);
				p.frames.playSound(p, 160, 1, 0);
				p.autoCast[0] = 4;
				p.autoCast[1] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
				playerM.autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 99, 100, 100, 5, p2.playerId, 4);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + modernLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
                break;

            case 10: // Wind Bolt
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= modernLvlReq[p.clickId]) {
                        if (hasReq(p, chaos, 1) && hasReq(p, air, 2)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                            	deleteItems(p, chaos, 1, air, 2);
				p.frames.playSound(p2, 218, 1, 0);
				p.frames.playSound(p, 218, 1, 0);
				p.autoCast[0] = 5;
				p.autoCast[1] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
				playerM.autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 117, 100, 118, 5, p2.playerId, 4);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + modernLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
                break;

            case 14: // Water Bolt
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= modernLvlReq[p.clickId]) {
                        if (hasReq(p, chaos, 1) && hasReq(p, air, 2) && hasReq(p, water, 2)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                            	deleteItems(p, chaos, 1, air, 2, water, 2);
				p.frames.playSound(p2, 209, 1, 0);
				p.frames.playSound(p, 209, 1, 0);
				p.autoCast[0] = 6;
				p.autoCast[1] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
				playerM.autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 120, 100, 121, 5, p2.playerId, 4);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + modernLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
                break;

            case 17: // Earth Bolt
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= modernLvlReq[p.clickId]) {
                        if (hasReq(p, chaos, 1) && hasReq(p, air, 2) && hasReq(p, earth, 3)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                            	deleteItems(p, chaos, 1, air, 2, earth, 3);
				p.frames.playSound(p2, 130, 1, 0);
				p.frames.playSound(p, 130, 1, 0);
				p.autoCast[0] = 7;
				p.autoCast[1] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
				playerM.autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 123, 100, 124, 5, p2.playerId, 4);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + modernLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
                break;

            case 20: // Fire Bolt
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= modernLvlReq[p.clickId]) {
                        if (hasReq(p, chaos, 1) && hasReq(p, air, 2) && hasReq(p, fire, 3)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                            	deleteItems(p, chaos, 1, air, 2, fire, 3);
				p.frames.playSound(p2, 157, 1, 0);
				p.frames.playSound(p, 157, 1, 0);
				p.autoCast[0] = 8;
				p.autoCast[1] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
				playerM.autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 126, 100, 127, 5, p2.playerId, 4);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + modernLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
                break;

               case 0: // Home Teleport
            if (p.homeTeleDelay <= 0) {
                p.homeTele = 15;
                p.normalHomeTele = true;
            } else if (p.homeTeleDelay > 0) {
                sendMsg(p, "You cannot teleport until you've waited 30 minutes.");
                p.magicDelay = mainDelayInt;
            }
            break;
            case 24: // Wind Blast
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= modernLvlReq[p.clickId]) {
                        if (hasReq(p, death, 1) && hasReq(p, air, 3)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                            	deleteItems(p, death, 1, air, 3);
				p.frames.playSound(p2, 216, 1, 0);
				p.frames.playSound(p, 216, 1, 0);
				p.autoCast[0] = 9;
				p.autoCast[1] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
				playerM.autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 132, 100, 133, 5, p2.playerId, 4);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + modernLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
                break;

            case 27: // Water Blast
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= modernLvlReq[p.clickId]) {
                        if (hasReq(p, death, 1) && hasReq(p, air, 3) && hasReq(p, water, 3)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                            	deleteItems(p, death, 1, air, 3, water, 3);
				p.frames.playSound(p2, 207, 1, 0);
				p.frames.playSound(p, 207, 1, 0);
				p.autoCast[0] = 10;
				p.autoCast[1] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
				playerM.autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 135, 100, 136, 5, p2.playerId, 4);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + modernLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
                break;

            case 33: // Earth Blast
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= modernLvlReq[p.clickId]) {
                        if (hasReq(p, death, 1) && hasReq(p, air, 3) && hasReq(p, earth, 4)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                            	deleteItems(p, death, 1, air, 3, earth, 4);
				p.frames.playSound(p2, 128, 1, 0);
				p.frames.playSound(p, 128, 1, 0);
				p.autoCast[0] = 11;
				p.autoCast[1] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
				playerM.autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 138, 100, 139, 5, p2.playerId, 4);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + modernLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
                break;

            case 38: // Fire Blast
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= modernLvlReq[p.clickId]) {
                        if (hasReq(p, death, 1) && hasReq(p, air, 4) && hasReq(p, fire, 5)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                            	deleteItems(p, death, 1, air, 4, fire, 5);
				p.frames.playSound(p2, 155, 1, 0);
				p.frames.playSound(p, 155, 1, 0);
				p.autoCast[0] = 12;
				p.autoCast[1] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
				playerM.autoCastEngine(casterX, casterY, offsetX, offsetY, 1162, 129, 100, 130, 5, p2.playerId, 4);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + modernLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
                break;

            case 45: // Wind Wave
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= modernLvlReq[p.clickId]) {
                        if (hasReq(p, blood, 1) && hasReq(p, air, 5)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                            	deleteItems(p, blood, 1, air, 5);
				p.frames.playSound(p2, 222, 1, 0);
				p.frames.playSound(p, 222, 1, 0);
				p.autoCast[0] = 13;
				p.autoCast[1] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
				playerM.autoCastEngine(casterX, casterY, offsetX, offsetY, 1167, 158, 100, 159, 5, p2.playerId, 4);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + modernLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
                break;

            case 48: // Water Wave
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= modernLvlReq[p.clickId]) {
                        if (hasReq(p, blood, 1) && hasReq(p, air, 5) && hasReq(p, water, 7)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                            	deleteItems(p, blood, 1, air, 5, water, 7);
				p.frames.playSound(p2, 213, 1, 0);
				p.frames.playSound(p, 213, 1, 0);
				p.autoCast[0] = 14;
				p.autoCast[1] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
				playerM.autoCastEngine(casterX, casterY, offsetX, offsetY, 1167, 161, 100, 162, 5, p2.playerId, 4);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + modernLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
                break;

            case 52: // Earth Wave
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= modernLvlReq[p.clickId]) {
                        if (hasReq(p, blood, 1) && hasReq(p, air, 5) && hasReq(p, earth, 7)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                            	deleteItems(p, blood, 1, air, 5, earth, 7);
				p.frames.playSound(p2, 134, 1, 0);
				p.frames.playSound(p, 134, 1, 0);
				p.autoCast[0] = 15;
				p.autoCast[1] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
				playerM.autoCastEngine(casterX, casterY, offsetX, offsetY, 1167, 164, 100, 165, 5, p2.playerId, 4);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + modernLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
                break;

            case 55: // Fire Wave
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= modernLvlReq[p.clickId]) {
                        if (hasReq(p, blood, 1) && hasReq(p, air, 5) && hasReq(p, fire, 7)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                            	deleteItems(p, blood, 1, air, 5, fire, 7);
				p.frames.playSound(p2, 162, 1, 0);
				p.frames.playSound(p, 162, 1, 0);
				p.autoCast[0] = 16;
				p.autoCast[1] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
				playerM.autoCastEngine(casterX, casterY, offsetX, offsetY, 1167, 155, 100, 156, 5, p2.playerId, 4);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + modernLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
                break;
              
            case 56: // Entangle
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= modernLvlReq[p.clickId]) {
                        if (hasReq(p, nature, 4) && hasReq(p, water, 5) && hasReq(p, earth, 5)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				Server.engine.playerMovement.resetWalkingQueue(p2);
				p.magicSpellHit = true;
				p2.freezeDelay = 20;
			    } else {
				p.magicSpellHit = false;
			    }
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                            	deleteItems(p, nature, 4, water, 5, earth, 5);
				p.frames.playSound(p2, 151, 1, 0);
				p.frames.playSound(p, 151, 1, 0);
				p.autoCast[0] = 20;
				p.autoCast[1] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
				playerM.autoCastEngine(casterX, casterY, offsetX, offsetY, 1161, 177, 100, 178, 5, p2.playerId, 4);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + modernLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
                break;
                
            case 12: // Bind
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= modernLvlReq[p.clickId]) {
                        if (hasReq(p, nature, 2) && hasReq(p, water, 3) && hasReq(p, earth, 3)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				Server.engine.playerMovement.resetWalkingQueue(p2);
				p.magicSpellHit = true;
				p2.freezeDelay = 6;
			    } else {
				p.magicSpellHit = false;
			    }
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                            	deleteItems(p, nature, 2, water, 3, earth, 3);
				p.frames.playSound(p2, 101, 1, 0);
				p.frames.playSound(p, 101, 1, 0);
				p.autoCast[0] = 18;
				p.autoCast[1] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
				playerM.autoCastEngine(casterX, casterY, offsetX, offsetY, 1161, 177, 100, 178, 5, p2.playerId, 4);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {			
			pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + modernLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
                break;

            case 30: // Bind
			stopMove(p);
                if (p.magicDelay <= 0 && p.combatDelay <= 0) {
                    if (magicLvl(p) >= modernLvlReq[p.clickId]) {
                        if (hasReq(p, nature, 3) && hasReq(p, water, 4) && hasReq(p, earth, 4)) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				Server.engine.playerMovement.resetWalkingQueue(p2);
				p.magicSpellHit = true;
				p2.freezeDelay = 15;
			    } else {
				p.magicSpellHit = false;
			    }
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                            	deleteItems(p, nature, 3, water, 4, earth, 4);
				p.frames.playSound(p2, 3003, 1, 0);
				p.frames.playSound(p, 3003, 1, 0);
				p.autoCast[0] = 19;
				p.autoCast[1] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				pm.setMagicPlayer(false);
				playerM.autoCastEngine(casterX, casterY, offsetX, offsetY, 1161, 177, 100, 178, 5, p2.playerId, 4);
                        } else {
				pm.setMagicPlayer(false);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                        }
                    } else {
				pm.setMagicPlayer(false);
                        sendMsg(p, "You need level " + modernLvlReq[p.clickId] + " magic to use this spell.");
                    }
                }
                break;

            case 41: // Saradomin Strike
			stopMove(p);
                if (p.combatDelay <= 0) {
                   if (magicLvl(p) >= 60) {
                      if (hasReq(p, 554, 2) && hasReq(p, 565, 2) && hasReq(p, 556, 4)) {
                         if (p.equipment[3] == 2415) {
                            if (p.equipment[1] != 2413 && p.equipment[1] != 2414 && p.equipment[3] != 2416 && p.equipment[1] != 2417) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                            	deleteItems(p, 554, 2, 565, 2, 556, 4);
				p.frames.playSound(p2, 1659, 1, 0);
				p.frames.playSound(p, 1659, 1, 0);
				p.autoCast[0] = 21;
				p.autoCast[1] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				p.autoCastDmgDelay = 3;
				p.autoCastDelay = 3;
				p.requestAnim(811, 0);
				pm.setMagicPlayer(false);
                            } else {
				pm.setMagicPlayer(false);
                                      face(p, p2.playerId + 32768);
                            	      sendMsg(p, "You cannot be wearing the cape or staff of another god while using this spell.");
                            }
                         } else {
				pm.setMagicPlayer(false);
                                   face(p, p2.playerId + 32768);
                            	   sendMsg(p, "You need to be wielding a Saradomin staff to cast this spell.");
                         }
                      } else {
				pm.setMagicPlayer(false);
                                face(p, p2.playerId + 32768);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                      }
                   } else {
				pm.setMagicPlayer(false);
                        face(p, p2.playerId + 32768);
                        sendMsg(p, "You need level 60 magic to use this spell.");
                   }
                }
                break;

            case 42: // Claws of Guthix
			stopMove(p);
                if (p.combatDelay <= 0) {
                   if (magicLvl(p) >= 60) {
                      if (hasReq(p, 554, 1) && hasReq(p, 565, 2) && hasReq(p, 556, 4)) {
                         if (p.equipment[3] == 2416) {
                            if (p.equipment[1] != 2412 && p.equipment[1] != 2414 && p.equipment[3] != 2415 && p.equipment[1] != 2417) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                            	deleteItems(p, 554, 1, 565, 2, 556, 4);
				p.frames.playSound(p2, 1653, 1, 0);
				p.frames.playSound(p, 1653, 1, 0);
				p.autoCast[0] = 22;
				p.autoCast[1] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				p.autoCastDmgDelay = 3;
				p.autoCastDelay = 3;
				p.requestAnim(811, 0);
				pm.setMagicPlayer(false);
                            } else {
				pm.setMagicPlayer(false);
                                      face(p, p2.playerId + 32768);
                            	      sendMsg(p, "You cannot be wearing the cape or staff of another god while using this spell.");
                            }
                         } else {
				pm.setMagicPlayer(false);
                                   face(p, p2.playerId + 32768);
                            	   sendMsg(p, "You need to be wielding a Guthix staff to cast this spell.");
                         }
                      } else {
				pm.setMagicPlayer(false);
                                face(p, p2.playerId + 32768);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                      }
                   } else {
				pm.setMagicPlayer(false);
                        face(p, p2.playerId + 32768);
                        sendMsg(p, "You need level 60 magic to use this spell.");
                   }
                }
                break;

            case 43: // Flames of Zamorak
			stopMove(p);
                if (p.combatDelay <= 0) {
                   if (magicLvl(p) >= 60) {
                      if (hasReq(p, 554, 4) && hasReq(p, 565, 2) && hasReq(p, 556, 1)) {
                         if (p.equipment[3] == 2417) {
                            if (p.equipment[1] != 2412 && p.equipment[1] != 2413 && p.equipment[3] != 2415 && p.equipment[1] != 2416) {
			    if (getRandom(magicAttack(p)) >= getRandom(magicDefence(p))) {
				p.magicSpellHit = true;
			    } else {
				p.magicSpellHit = false;
			    }
                                face(p, p2.playerId + 32768);
                                stopMove(p);
                            	deleteItems(p, 554, 1, 565, 2, 556, 4);
				p.frames.playSound(p2, 1655, 1, 0);
				p.frames.playSound(p, 1655, 1, 0);
				p.autoCast[0] = 23;
				p.autoCast[1] = 0;
				p.combatDelay = 6;
				p.spellType = p.interfaceId;
				p.autoCastDmgDelay = 3;
				p.autoCastDelay = 3;
				p.requestAnim(811, 0);
				pm.setMagicPlayer(false);
                            } else {
				pm.setMagicPlayer(false);
                                      face(p, p2.playerId + 32768);
                            	      sendMsg(p, "You cannot be wearing the cape or staff of another god while using this spell.");
                            }
                         } else {
				pm.setMagicPlayer(false);
                                   face(p, p2.playerId + 32768);
                            	   sendMsg(p, "You need to be wielding a Zamorak staff to cast this spell.");
                         }
                      } else {
				pm.setMagicPlayer(false);
                                face(p, p2.playerId + 32768);
                            	sendMsg(p, "You dont have the required runes for this spell.");
                      }
                   } else {
				pm.setMagicPlayer(false);
                        face(p, p2.playerId + 32768);
                        sendMsg(p, "You need level 60 magic to use this spell.");
                   }
                }
                break;

            default:
                Misc.println("Unhandled Modern button: " + p.clickId + ".");
            }
		pm.setMagicPlayer(false);
            break;

        default:
            Misc.println(
                    "PlayerID " + playerId + " - InterfaceID " + interfaceId
                    + " - ButtonID " + p.clickId + ".");

        }
      }
    }

    public static int magicAttack(Player p) {
		if (p == null) {
			return 0;
		}
		return (p.skillLvl[6] + p.equipmentBonus[3] + p.equipmentBonus[3]);
	}

    public static int magicDefence(Player p) {
			Player p2 = Server.engine.players[p.enemyIndex];
		if (p == null) {
			return 0;
		}
	    	if (!p.magicPrayer) {
			return (int)((p.skillLvl[1] / 3) + (p2.skillLvl[6] / 2) + p.equipmentBonus[8]);
	    	} else if (p.magicPrayer) {
			return (int)((p.skillLvl[1] / 2) + (p2.skillLvl[6] / 4) + p.equipmentBonus[8] + 50);
	    	} else {
			return 0;
		}
	}

    public static int getRandom(int range) {
        return (int)(Math.random() * (range + 1));
    }
    
    public static int[] ancientLvlReq = {
        58, 82, 70, 94, 56, 80, 68, 92, 50, 74, 62, 86, 52, 76, 64, 88, 54, 60,
        66, 72, 78, 84, 90, 96, 0
    };
    
    public static int[] ancientSpellXp = {
        34, 46, 40, 52, 33, 45, 39, 51, 30, 42, 36, 48, 31, 43, 37, 49, 64, 70,
        76, 82, 88, 94, 100, 106, 0
    };
    
    public static int[] ancientMaxHit = {
        18, 26, 22, 30, 17, 25, 21, 29, 15, 23, 19, 27, 16, 24, 20, 28, 0, 0, 0,
        0, 0, 0, 0, 0, 0
    };
    
    public static double[] modernSpellXp = {
        0, 5.5, 13, 0, 7.5, 9.5, 21, 11.5, 25, 13.5, 29, 30, 31, 16.5, 35, 37,
        19.5, 41, 45, 43, 22.5, 48, 24.5, 30, 25.5, 53, 55.5, 28.5, 59, 30, 60,
        30, 61, 31.5, 65, 66, 67, 68, 35.5, 70, 35.5, 35, 35, 35, 68, 36, 73, 74,
        37.5, 76, 76, 78, 40, 83, 84, 42.5, 89, 90, 180, 92, 97, 100
    };
    
    public static int[] modernLvlReq = {
        0, 1, 3, 4, 5, 7, 9, 11, 13, 15, 17, 19, 20, 21, 23, 25, 27, 29, 31, 32,
        33, 35, 37, 39, 40, 41, 43, 45, 47, 49, 50, 50, 50, 51, 53, 55, 56, 57,
        58, 59, 60, 60, 60, 60, 60, 61, 62, 63, 64, 65, 66, 66, 68, 70, 73, 74,
        75, 79, 80, 80, 82, 87, 90
    };
    
    public static int[] modernMagicMaxHit = {
        0, 2, 0, 0, 4, 0, 6, 0, 8, 0, 9, 0, 0, 0, 10, 0, 0, 11, 0, 0, 12, 0, 15,
        0, 13, 0, 0, 14, 25, 2, 19, 19, 0, 15, 0, 0, 0, 0, 16, 0, 0, 20, 20, 20,
        0, 17, 0, 0, 18, 0, 0, 0, 19, 0, 0, 20, 0, 0, 0, 0, 0, 0
    };
    
}